<?php
/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2017/10/5 0005
 * Time: 下午 9:51
 */

namespace src;


use conf\Config;

class DownLoad
{


    public function get($url,$handle="html"){
        #only one url
        static $static_page=[];
        if(in_array($url,$static_page)){
            return -1;
        }else{
            $static_page[]=$url;
        }
        #url build
        if(substr($url,0,1) == "/"){
            $url = sprintf("%s/%s",Config::DOMAIN,$url);
        }

        #######################
        if (strpos($url, "ask.39.net") <= 0) {
            return false;
        }
        echo "download get $url \n";
        ##########################################
        $hash = md5($url);
        $dir = sprintf("%s/%s",Config::$PATH_TMP,$handle);
        if(!is_dir($dir)){
            $result = mkdir($dir);
            if(!$result){
                exit("program end because $dir can not be establish");
            }
        }
        $path = sprintf("%s/%s.html",$dir,$hash);
        if(is_file($path)){
            $html = file_get_contents($path);
        }else{
            $html = file_get_contents($url);
            file_put_contents($path,$html);
        }
        return new Item($hash,$url,$html,$handle);
    }
}